
// Fix indexOf for browsers that do not have it
if (!Array.prototype.indexOf)
{
	Array.prototype.indexOf = function (obj, start)
	{
		for (var i = (start || 0); i < this.length; i++)
		{
			if (this[i] == obj)
				return i;
		}
		return -1;
	}
}

// this function will ensure that the best method for adding an event to a control is used based on the browser specifics
// ex: CrossBrowserAddEventHandler(window, "load", do_swfupload_for_x);
function CrossBrowserAddEventHandler(target, eventName, handlerName)
{
    // if an ID passed in, get the element
    if(typeof(target) == "string")
        target = document.getElementById(element);

    // make sure the event name is not like 'on'something because of the addEventListener way of dealing with event names
    eventName = eventName.toLowerCase().replace(/^on/, "");

    // mouse wheel event uses a different name in FF
    if (eventName == 'mousewheel' && isFF)
        eventName = 'DOMMouseScroll';
        
    // get the event handler
    var eventHandler = getEventHandler(handlerName);
    
    // if browser supports addEventListener, use it
    if (target.addEventListener)
    {
        target.addEventListener(eventName, eventHandler, false);
    }
    // if browser supports attachEvent, use it
    else if (target.attachEvent)
    {
        target.attachEvent("on" + eventName, eventHandler);
    }
    // else, use the basic way of attaching an event handler
    else
    {
        // get the existing handler for this event
        var originalHandler = target["on" + eventName];
        // if something exists
        if (originalHandler)
            // add a function to call the original handler and then our own handler
            target["on" + eventName] = function(e) { originalHandler(e); eventHandler(e); };
        else
            // else just set our own handler as the handler for thing event
            target["on" + eventName] = function(e) { eventHandler(e); };
    }
}

// this function will ensure that the best method for adding an event to a control is used based on the browser specifics
function CrossBrowserRemoveEventHandler(target, eventName, handlerName)
{
    // if an ID passed in, get the element
    if(typeof(target) == "string")
        target = document.getElementById(element);

    // make sure the event name is not like 'on'something because of the removeEventListener way of dealing with event names
    eventName = eventName.toLowerCase().replace(/^on/, "");

    // mouse wheel event uses a different name in FF
    if (eventName == 'mousewheel' && isFF)
        eventName = 'DOMMouseScroll';

        
    // get the event handler
    var eventHandler = getEventHandler(handlerName);

    // if browser supports addEventListener, use it
    if (target.removeEventListener)
        target.removeEventListener(eventName, eventHandler, false);
    // if browser supports attachEvent, use it
    else if (target.detachEvent)
        target.detachEvent("on" + eventName, eventHandler);
    // else, use the basic way of attaching an event handler
    else
    {
        // get the existing handler for this event
        var originalHandler = target["on" + eventName];
        
        // if something exists
        if (originalHandler)
        {
            // if this is the only function for the event
            if (target["on" + eventName].toString() == eventHandler.toString())
                // remove it
                target["on" + eventName] = null;
            // else, we have multiple, so this is going to be very tricky...
            else
            {
                // first we need to get the name of the handler in case it's not passed in as a function but not a name
                var handlerName = typeof handlerName == "string" ? handlerName : ((handlerName = handlerName.toString().match(/^\s*function\s+([^\s\(]+)/)) ? handlerName[1] : "");
                
                // if the function name was found (meaning it's not an anonymous function)
                if (handlerName != "")
                    // replace it from the body of the event handler of this element
                    eval("target['on" + eventName + "'] = " + eval("target['on" + eventName + "'].replace(/" + handlerName + "\\s*\\([^\\)]*\\)\\s*;?\\s*/i, '')"));
                    
                // else we can't identify it... so we can't remove it. lame browser + advanced coding = unexpected results
            }
        }
    }
}

function getEventHandler(handlerName)
{
    // build the event handler
    var eventHandler = null;
    if (typeof handlerName == "string")
    	eventHandler = new Function("e", "if (!e) {e = window.event;} " + handlerName + "(e)");
    else if (typeof handlerName == "function")
    	eventHandler = function(e) {if (!e) {e = window.event;} handlerName(e);}
    else 
    	throw ("Handler is not a function or the name of a function!");
    	
    return eventHandler;
}

function urlencode (str)
{
    var hexStr = function (dec) {
        return '%' + dec.toString(16).toUpperCase();
    };
 
    var ret = '',
            unreserved = /[\w.-]/;
    str = (str+'').toString();
 
    for (var i = 0, dl = str.length; i < dl; i++) {
        var ch = str.charAt(i);
        if (unreserved.test(ch)) {
            ret += ch;
        }
        else {
            var code = str.charCodeAt(i);
            // Reserved assumed to be in UTF-8, as in PHP
            if (code === 32) {
                ret += '+'; // %20 in rawurlencode
            }
            else if (code < 128) { // 1 byte
                ret += hexStr(code);
            }
            else if (code >= 128 && code < 2048) { // 2 bytes
                ret += hexStr((code >> 6) | 0xC0);
                ret += hexStr((code & 0x3F) | 0x80);
            }
            else if (code >= 2048 && code < 65536) { // 3 bytes
                ret += hexStr((code >> 12) | 0xE0);
                ret += hexStr(((code >> 6) & 0x3F) | 0x80);
                ret += hexStr((code & 0x3F) | 0x80);
            }
            else if (code >= 65536) { // 4 bytes
                ret += hexStr((code >> 18) | 0xF0);
                ret += hexStr(((code >> 12) & 0x3F) | 0x80);
                ret += hexStr(((code >> 6) & 0x3F) | 0x80);
                ret += hexStr((code & 0x3F) | 0x80);
            }
        }
    }
    return ret;
}

